package cn.edu.cqu.bysj.wm.gui.table;


import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;

import mySwingToolkit.controller.TableController;
import mySwingToolkit.model.ClassModel;
import mySwingToolkit.model.SwitchedTableModel;
import mySwingToolkit.ui.TableView;

import org.neo4j.graphdb.Node;

import scala.collection.mutable.StringBuilder;
import cn.edu.cqu.bysj.wm.def.ConstDef;
import cn.edu.cqu.bysj.wm.gui.result.CommunityRow;

public class CommunityView extends TableView<CommunityRow>{
	/**
	 * create community view tabel
	 * @param title
	 * @param size
	 */
	public CommunityView(String title,int size){
		//构造
		super(new SwitchedTableModel<CommunityRow>(
				new Object[][] {
				},
				new String[] {
						"社区id", "社区大小", "聚类系数", "社区强度","社区成员"
				},
				new Class[] {
						Long.class, Integer.class, Double.class, String.class,ContentButton.class
				},
				new boolean[] {
						false, false, false, false,false
				},
				false, false, false,size
				){
			@Override
			public Object[] translate(
					ClassModel<CommunityRow> object) {
				CommunityRow c=object.getObject();
				String strength=null;
				switch (c.getStrength()) {
				case ConstDef.STRONG_COMMUNITY:
					strength="Strong";
					break;
				case ConstDef.WEAK_COMMUNITY:
					strength="Weak";
					break;
				case ConstDef.NEITHER:
					strength="Neither";
				default:
					break;
				}
				return new Object[]{object,this.getRowCount()+1,c.getCid(),c.getSize(),c.getCluster(),strength,new ContentButton(c.getMembers())};
			}
			@Override
			public CommunityRow constructObject(int row,
					int col) {
				@SuppressWarnings("unchecked")
				ClassModel<CommunityRow> cm=(ClassModel<CommunityRow>) this.getValueAt(row, 0);
				CommunityRow cr=null;
				if(cm!=null){
					cr=cm.getObject();
				}else{
					cr=new CommunityRow();
				}
				switch(col){
				case 2:
					cr.setCid((Long)this.getValueAt(row, 2));
					break;
				case 3:
					cr.setSize((Integer)this.getValueAt(row, 3));
					break;
				case 4:
					cr.setCluster((Integer)this.getValueAt(row, 4));
					break;
				case 5:
					cr.setStrength((Integer)this.getValueAt(row, 5));
					break;
				case 6:
					break;
				}
				return cr;
			}
			@Override
			public void initNullRow(int row) {
				//this.setValueAt("男", row, 4);
			}
		},title
		);
		//加入数据
		//this.initDatas(Collections.singletonList(new CommunityRow()));
		//设置tc
		this.setTableController(new TableController<CommunityRow>(){
			@Override
			public List<CommunityRow> getBackupDatas(
					SwitchedTableModel<CommunityRow> tm) {
				JOptionPane.showMessageDialog(null, "暂不支持");
				return null;
			}
			@Override
			public boolean saveChanges(SwitchedTableModel<CommunityRow> tm) {
				System.out.println(tm.getInsertDatas().size());
				return true;
			}
		});
		
		//添加render
		this.getTable().getColumnModel().getColumn(6).setCellRenderer(new ContentButton());
		this.getTable().addMouseListener(new JTableButtonMouseListener(this.getTable()));
	}
	
	static class ContentButton extends JButton implements ActionListener,TableCellRenderer{
		List<Node> members;
		public ContentButton(List<Node> members){
			this.members=members;
			this.addActionListener(this);
		}
		public ContentButton(){
			//this.members=members;
			this.addActionListener(this);
			setText("查看社区内成员");
		}
		public ContentButton(String title){
			//this.members=members;
			this.addActionListener(this);
			setText(title);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			StringBuilder temp=new StringBuilder();
			int num=0;
			for(Node node:members){
				num++;
				temp.append(node.getId()+"   ");
				if(num%10==0){
					temp.append("\n");
				}
			}
			JOptionPane.showMessageDialog(getParent(),temp.toString(), "社区内成员ID",JOptionPane.INFORMATION_MESSAGE);
		}
		@Override
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			if (isSelected) {
				setForeground(table.getSelectionForeground());
				setBackground(table.getSelectionBackground());
			} else {
				setForeground(table.getForeground());
				setBackground(UIManager.getColor("Button.background"));
			}
			return this;
		}
		
		
	}
	 class JTableButtonMouseListener extends MouseAdapter {
		  private final JTable table;
				
		  public JTableButtonMouseListener(JTable table) {
		    this.table = table;
		  }

		  @Override public void mouseClicked(MouseEvent e) {
		    int column = table.getColumnModel().getColumnIndexAtX(e.getX());
		    int row    = e.getY()/table.getRowHeight(); 

		    if (row < table.getRowCount() && row >= 0 && column ==6) {
		      Object value = table.getValueAt(row, column);
		      if (value instanceof JButton) {
		        ((JButton)value).doClick();
		      }
		    }
		  }
		}
	

	
}
